﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessHandler;
using EasyCode.Utility;
public partial class Page_Login : System.Web.UI.Page
{
    private UserController userController;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button_Login_Click(object sender, EventArgs e)
    {
        User user = new User();
        user.UserName = this.TextBox_UserName.Text.Trim();
        user.UserPassword = Commons.MD5(this.TextBox_Password.Text.Trim());
        user.UserStatus = true;
        userController = UserController.Instance;
        user = ( from o in userController.Users where o.UserName==user.UserName && o.UserPassword==user.UserPassword && o.UserStatus==true select o).SingleOrDefault();
        if (user != null)
        {
            user.LastLoginTime = DateTime.Now;
            userController.SubmitChanges();
            GlobalVariable.CurrentUserName = user.UserName;
            GlobalVariable.CurrentUserID = user.UserID;
            if (user.UserType.Value == (int)UserType.Admin)
            {
                GlobalVariable.isAdmin = true;
            }
            if (Request.QueryString["returnURL"] != null)
            {
                Response.Redirect(ResolveUrl(Request.QueryString["returnURL"]));
            }
            Response.Redirect(ResolveUrl("Default.aspx"));
        }
        else
        {
            this.Message_Alert.SetError(Resources.SystemAccount.InliadUserNameOrPass);
        }
    }
}